﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entidades.Espectaculos;
using System.Data.SqlClient;
using System.Configuration;

namespace Datos.Espectaculos
{
    public class EspRepresentanteDAL
    {
        private static EspRepresentanteDAL _instance;

        public static EspRepresentanteDAL Instance()
        {
            if (_instance == null)
            {
                _instance = new EspRepresentanteDAL();
            }

            return _instance;
        }

        public static string  cnx = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();

        public List<EspRepresentante> ListarRepresentante(int estado, int idRepresentante, string razonSocial, string ruc )
        {
           
            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand("[dbo].[sp_Esp_ListarRepresentante]", con);
            com.Parameters.AddWithValue("@Estado", estado == -1 ? (object)DBNull.Value : estado);
            com.Parameters.AddWithValue("@IdRepresentante", idRepresentante == -1 ? (object)DBNull.Value : idRepresentante);
            com.Parameters.AddWithValue("@RazonSocial", razonSocial == "" ? (object)DBNull.Value : razonSocial);
            com.Parameters.AddWithValue("@RUC", ruc == "" ? (object)DBNull.Value : ruc);
           
            com.CommandType = System.Data.CommandType.StoredProcedure;
            try
            {
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                List<EspRepresentante> lista = new List<EspRepresentante>();
                EspRepresentante obj = null;
                EspAuditoria oAudi = null;
                while (dr.Read())
                {
                    obj = new EspRepresentante();
                    obj.IdRepresentante = Convert.ToInt32(dr["IdRepresentante"].ToString());
                    obj.RazonSocial = dr["RazonSocial"].ToString();
                    obj.Correo = dr["Correo"].ToString();
                    obj.Direccion = dr["Direccion"].ToString();
                    obj.Telefono = dr["Telefono"].ToString();
                    obj.RUC = dr["RUC"].ToString();
                    obj.Observacion = dr["Observaciones"].ToString();
                    obj.Estado = Convert.ToInt32(dr["Estado"].ToString());
                    oAudi = new EspAuditoria(dr["user_creacion"].ToString(), dr["user_modificacion"].ToString(), dr["fecha_creacion"].ToString(), dr["fecha_modificacion"].ToString());

                    lista.Add(obj);
                }
                dr.Close();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();

            }
        }

        public EspRepresentante ObtenerRepresentante(int idRepresentante)
        {

            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand("[dbo].[sp_Esp_ListarRepresentante]", con);
            com.Parameters.AddWithValue("@Estado", (object)DBNull.Value );
            com.Parameters.AddWithValue("@IdRepresentante", idRepresentante);
            com.Parameters.AddWithValue("@RazonSocial",(object)DBNull.Value);
            com.Parameters.AddWithValue("@RUC",(object)DBNull.Value);

            com.CommandType = System.Data.CommandType.StoredProcedure;
            try
            {
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                EspRepresentante obj = null;
                EspAuditoria oAudi = null;
                while (dr.Read())
                {
                    obj = new EspRepresentante();
                    obj.IdRepresentante = Convert.ToInt32(dr["IdRepresentante"].ToString());
                    obj.RazonSocial = dr["RazonSocial"].ToString();
                    obj.Correo = dr["Correo"].ToString();
                    obj.Direccion = dr["Direccion"].ToString();
                    obj.Telefono = dr["Telefono"].ToString();
                    obj.RUC = dr["RUC"].ToString();
                    obj.Observacion = dr["Observaciones"].ToString();
                    obj.Estado = Convert.ToInt32(dr["Estado"].ToString());
                    oAudi = new EspAuditoria(dr["user_creacion"].ToString(), dr["user_modificacion"].ToString(), dr["fecha_creacion"].ToString(), dr["fecha_modificacion"].ToString());
                }
                dr.Close();

                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();

            }
        }

        public int AgregarRepresentante(EspRepresentante obj)
        {
            //string cnx = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand("[dbo].[sp_Esp_AgregarRepresentante]", con);

            com.Parameters.AddWithValue("@razonSocial", obj.RazonSocial);
            com.Parameters.AddWithValue("@correo", obj.Correo);
            com.Parameters.AddWithValue("@direccion", obj.Direccion);
            com.Parameters.AddWithValue("@telefono", obj.Telefono);
            com.Parameters.AddWithValue("@observaciones", obj.Observacion);
            com.Parameters.AddWithValue("@estado", obj.Estado);
            com.Parameters.AddWithValue("@RUC", obj.RUC);
            com.Parameters.AddWithValue("@user_creacion", obj.Auditoria.USER_CREACION);

            SqlParameter p = new SqlParameter();
            p.Direction = System.Data.ParameterDirection.Output;
            p.Value = 0;
            p.ParameterName = "@newIdRepresentante";

            com.Parameters.Add(p);

            com.CommandType = System.Data.CommandType.StoredProcedure;
            try
            {
                con.Open();
                com.Transaction = con.BeginTransaction();
                int result = com.ExecuteNonQuery();
                if (result > 0)
                {
                    result = Convert.ToInt32(com.Parameters["@newIdRepresentante"].Value);
                    com.Transaction.Commit();
                }
                else
                {
                    com.Transaction.Rollback();
                }

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }

        public int ModificarRepresentante(EspRepresentante obj)
        {
            //string cnx = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand("[dbo].[sp_Esp_ModificarRepresentante]", con);

            com.Parameters.AddWithValue("@IdRepresentante", obj.IdRepresentante);
            com.Parameters.AddWithValue("@razonSocial", obj.RazonSocial);
            com.Parameters.AddWithValue("@correo", obj.Correo);
            com.Parameters.AddWithValue("@direccion", obj.Direccion);
            com.Parameters.AddWithValue("@telefono", obj.Telefono);
            com.Parameters.AddWithValue("@observaciones", obj.Observacion);
            com.Parameters.AddWithValue("@estado", obj.Estado);
            com.Parameters.AddWithValue("@RUC", obj.RUC);
            com.Parameters.AddWithValue("@user_modificacion", obj.Auditoria.USER_MODIFICACION);

            com.CommandType = System.Data.CommandType.StoredProcedure;
            try
            {
                con.Open();
                com.Transaction = con.BeginTransaction();
                int result = com.ExecuteNonQuery();
                if (result > 0)
                {
                    com.Transaction.Commit();
                }
                else
                {
                    com.Transaction.Rollback();
                }

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }

    }
}
